package org.example;

import java.util.Timer;

public class Main {
    volatile int i = 1;
    //int i = 1;   加不加volatile的结果不同
    void volatileTest() {
        System.out.println("开始");
        while(i==1){
            //Thread.sleep(1);  //sleep就来得及刷新了
        }
        System.out.println("结束");
    }
    public static void main(String[] args) {
        Main main = new Main();
        new Thread(main::volatileTest).start();
        try {
            Thread.sleep(10L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        main.i=2;
        System.out.println("设置i值完成");
    }
}

class hello {
    static volatile int a = 0;//即使加了也不能保证两个写的可见性

    public static void main(String[] args) {

        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                a++;
            }).start();
            new Thread(()->{
                a++;
            }).start();
        }
        System.out.println(a);
    }
}



